package controller

import (
	"io/ioutil"

	"github.com/bitly/go-simplejson"

	"im/dao"
	"im/model"
)

type Systems struct {
	Base
}

func (s Systems) GetConfigBy(configName string) {
	config, err := dao.NewSystemConfigs().GetConfig(configName)
	if err != nil {
		s.SendCry(err)
		return
	}
	json, err := simplejson.NewJson([]byte(config))
	if err != nil {
		s.SendCry(err)
		return
	}
	s.SendSmile(json)
}

func (s Systems) PutConfigBy(configName string) {
	body := s.Ctx.Request().Body
	config, err := ioutil.ReadAll(body)
	if err != nil {
		s.SendCry(err)
		return
	}
	err = dao.NewSystemConfigs().UpdateConfig(configName, string(config))
	if err != nil {
		s.SendCry(err)
		return
	}
	s.SendSmile(nil)
}

func (s Systems) GetChannels() {
	channels, err := dao.NewChannels().ListConfigs()
	if err != nil {
		s.SendCry(err)
		return
	}
	s.SendSmile(channels)
}
func (s Systems) PutChannelsBy(channelName string) {
	var m model.Channels
	s.Ctx.ReadJSON(&m)
	var err = dao.NewChannels().UpdateChannel(channelName, m)
	if err != nil {
		s.SendCry(err)
		return
	}
	s.SendSmile(nil)
}
func (s Systems) DeleteChannelsBy(channelName string) {
	var err = dao.NewChannels().DeleteChannel(channelName)
	if err != nil {
		s.SendCry(err)
		return
	}
	s.SendSmile(nil)
}
func (s Systems)PostChannels(){
	var m model.Channels
	s.Ctx.ReadJSON(&m)
	var err = dao.NewChannels().Create(m)
	if err != nil {
		s.SendCry(err)
		return
	}
	s.SendSmile(nil)
}
